package com.pointstar.dp.software;

import java.util.HashMap;
import java.util.Map;

/**
 * 15年：罗马数字转十进制数
 *
 * @author 朱科睿
 */
public class RomanToInt {

    protected static Map<Character, Integer> map = new HashMap<>();

    static {
        map.put('I', 1);
        map.put('V', 5);
        map.put('X', 10);
        map.put('L', 50);
        map.put('C', 100);
        map.put('D', 500);
        map.put('M', 1000);
        map.put('O', 0);
    }

    public static int romanToInt(String roman) {

//        roman += 'O';
        int sum = 0;
        char[] chars = roman.toCharArray();
        int cur;
        int next;
        for (int i = 0; i < chars.length - 1; i++) {
            cur = map.get(chars[i]);
            next = map.get(chars[i + 1]);
            if (cur == next) {
                sum += next;
            } else if (cur < next) {
                sum = cur;
            } else {
                sum += cur;
            }
        }

        return sum;
    }

    public static void main(String[] args) {
        System.out.println(romanToInt("XCIX"));
    }

}
